﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using ReagentStore.ERM.Properties;
using ReagentStore.UTL;

namespace ReagentStore
{
	public delegate void VerifyPasswordHandler();

	public partial class LogonForm : Form
	{
		private bool _expanded;

		public LogonForm(VerifyPasswordHandler handler)
		{
			InitializeComponent();
			_verify += handler;
		}

		public event VerifyPasswordHandler _verify;

		private void buttonOK_Click(object sender, EventArgs e)
		{
			Settings.Default.ServerName = textServer.Text;
			Settings.Default.Save();

			VerifyPasswordHandler handler = _verify;
			if (handler != null)
				handler();
		}

		private void buttonOption_Click(object sender, EventArgs e)
		{
			_expanded = !_expanded;

			if (_expanded)
				Expand();
			else
				Collapse();
		}

		private void Expand()
		{
			Height += panelServer.Height;
			buttonOption.Text = "选项 <<";
		}

		private void Collapse()
		{
			Height -= panelServer.Height;
			buttonOption.Text = "选项 >>";
		}

		private void LogonForm_Load(object sender, EventArgs e)
		{
			textServer.Text = Settings.Default.ServerName;
			Collapse();
		}

		private void textPWD_Validating(object sender, CancelEventArgs e)
		{
			string encrypted = DESEncryptor.Encrypt(textPWD.Text);

			if (!encrypted.Equals(Properties.Settings.Default.Password))
			{
				errorProvider.SetError(textPWD, "密码错误");
				e.Cancel = true;
			}
			else
				errorProvider.SetError(textPWD, string.Empty);
		}
	}
}